package com.css.fxfzfxqh.modules.lifelineEngineering.repository.impl;

import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.PageParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.RegionParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.LifelineDataRepository;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.InfluenceFieldVo;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.LifelinePageVo;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LifelineDataRepositoryImpl implements LifelineDataRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> page(RegionParam param, int page, int size) {
        Integer type = param.getType();
        /*
        已有数据   储气罐   桥梁  通信设备室内   地下管网   重要发电厂房   水厂水池   变电站   水厂泵房   （道路，电缆线路  无经纬度信息）
        需要数据   地下管网  水厂泵房 水厂水池   燃气-储气罐   重要发电厂房  电缆线路  变电站  室内通信设备   道路  桥梁

        供气管网   燃气-门店/场站  基站  无     电视塔  架空线路 用不到
        type 1:地下管网   2:水厂泵房  3:水厂水池  4:燃气-储气罐    5:桥梁    6:道路    7:变电站 8:架空线路  9:电缆线路
        10:发电厂  11:通信室内设备   12:电视塔
         */
        StringBuilder sql = new StringBuilder("select ");
        if (type == 1) {
            //地下管网
            sql.append(" cdpn_id as id,province,cdpn_name as data_name,version,version_info,create_user,create_time from czt_dx_pipe_network");
        } else if (type == 2) {
            //水厂水池
            //TODO 无名字
            sql.append(" cp_id as id,province,style as data_name,version,version_info,create_user,create_time from czt_pool");
        } else if (type == 3) {
            //水厂泵房
            sql.append(" cw_id as id,province,building_name as data_name,version,version_info,create_user,create_time from czt_waterworks");
        } else if (type == 4) {
            //生命线-储气罐
            sql.append(" car_id as id,province,car_name as data_name,version,version_info,create_user,create_time from czt_air_receiver");
        } else if (type == 5) {
            //TODO 供气管网
        } else if (type == 6) {
            //TODO 燃气-门店/场站
        } else if (type == 7) {
            //重要发电厂房
            sql.append(" cip_id as id,province,cip_name as data_name,version,version_info,create_user,create_time from czt_important_powerhouse");
        } else if (type == 8) {
            //TODO 供电范围矢量数据
        } else if (type == 9) {
            //电缆线路
            sql.append(" ccr_id as id,province,ccr_name as data_name,version,version_info,create_user,create_time from czt_cable_route");
        } else if (type == 10) {
            //变电站
            sql.append(" cts_id as id,province,cts_name as data_name,version,version_info,create_user,create_time from czt_transformer_substation");
        } else if (type == 11) {
            //道路
            sql.append(" cr_id as id,province,cr_name as data_name,version,version_info,create_user,create_time from czt_road");
        } else if (type == 13) {
            //TODO 基站
        } else if (type == 14) {
            //TODO 基站通信服务范围矢量数据
        } else if (type == 15) {
            sql.append(" ccie_id as id,province,in_object_name as data_name,version,version_info,create_user,create_time from czt_comm_indoor_equipment");
        }
        Map<String, Object> map = new HashMap<>();
        StringBuilder whereSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        StringBuilder limitSql = new StringBuilder();
        if (type != 12) {//不为桥梁时
            whereSql.append(" where del_flag= '0' ");
            if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
                whereSql.append(" and province = '" + param.getProvince() + "'");
            }
            if (!PlatformObjectUtils.isEmpty(param.getCity())) {
                whereSql.append(" and city = '" + param.getCity() + "'");
            }
            if (!PlatformObjectUtils.isEmpty(param.getCounty())) {
                whereSql.append(" and county = '" + param.getCounty() + "'");
            }
            orderSql.append(" order by create_time desc");
            limitSql.append(" limit " + size + " offset " + (page - 1) * size);
            Integer total = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(Object.class)).size();
            List<LifelinePageVo> list = jdbcTemplate.query(sql.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(LifelinePageVo.class));
            map.put("total", total);
            map.put("rows", list);
        } else {
            StringBuilder pointSql = new StringBuilder("select cb_id as id,province,cb_name as data_name,create_user,create_time from czt_bridge_point ");//union
            StringBuilder lineSql = new StringBuilder(" union select cb_id as id,province,cb_name as data_name,create_user,create_time from czt_bridge_line ");
            StringBuilder pointWhereSql = new StringBuilder(" where del_flag = '0'");
            StringBuilder lineWhereSql = new StringBuilder(" where del_flag = '0'");
            StringBuilder bridgeOrderSql = new StringBuilder("  order by create_time ");
            if (!PlatformObjectUtils.isEmpty(param.getProvince())) {
                pointWhereSql.append(" and province = '" + param.getProvince() + "'");
                lineWhereSql.append(" and province = '" + param.getProvince() + "'");
            }
            if (!PlatformObjectUtils.isEmpty(param.getCity())) {
                pointWhereSql.append(" and city = '" + param.getCity() + "'");
                lineWhereSql.append(" and city = '" + param.getCity() + "'");
            }
            if (!PlatformObjectUtils.isEmpty(param.getCounty())) {
                pointWhereSql.append(" and county = '" + param.getCounty() + "'");
                lineWhereSql.append(" and county = '" + param.getCounty() + "'");
            }
            String bridgeSql = pointSql.append(pointWhereSql).append(lineSql).append(lineWhereSql).append(bridgeOrderSql).toString();
            Integer total = jdbcTemplate.query(bridgeSql, new BeanPropertyRowMapper<>(Object.class)).size();
            map.put("total", total);
            StringBuilder bridgeLimitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * page);
            List<LifelinePageVo> list = jdbcTemplate.query(bridgeSql + bridgeLimitSql.toString(), new BeanPropertyRowMapper<>(LifelinePageVo.class));
            map.put("rows", list);
        }
        return map;
    }

    @Override
    public Map<String, Object> influenceField(PageParam param, int page, int size) {
        StringBuilder sql = new StringBuilder("select distinct (version_code), assess_user_name,to_char(assess_time, 'YYYY-MM-DD HH24:MI:SS') as assess_time FROM fxqh_setting_seismic_influenceflied WHERE del_flag = '0'");
        if(PlatformObjectUtils.isNotEmpty(param.getCode())){
            sql.append(" and version_code like '%"+param.getCode()+"%'");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getUserName())){
            sql.append(" and assess_user_name like '%"+param.getUserName()+"%'");
        }
        StringBuilder orderSql = new StringBuilder(" order by assess_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(InfluenceFieldVo.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<InfluenceFieldVo> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(InfluenceFieldVo.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }
}
